<?php

class App_Controller_Action extends Zend_Controller_Action
{
    public $_urlbase;
    public $_modulo = 'painel';
    public $_titulo = 'Guapo Web';
    public $db = ''; 
    
    public function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array())
    {
        parent::__construct($request, $response);
        $this->auth(); // validação de sessão do login
        
        $this->_urlbase = $this->getFrontController()->getBaseUrl();
        $this->_controller  = $this->getRequest()->getControllerName();
        $this->_action = $this->getRequest()->getActionName();
        $this->_urlmodulo = $this->_urlbase.'/'.$this->_modulo;
        
        $this->view->urlbase = $this->_urlbase;
        $this->view->controller  = $this->_controller;
        $this->view->modulo  = $this->_modulo;
        $this->view->action  = $this->_action;
        $this->view->urlmodulo = $this->_urlmodulo;
        
        $this->db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $this->view->db  = $this->db;
        
        $this->_helper->layout->setLayout("layout_admin");
        
        $perm_modulo = FALSE;
        $sessao = new Zend_Session_Namespace('Modulos');
        if(isset($sessao->modulos[$this->_controller]) and strlen($sessao->modulos[$this->_controller]) > 0)
        {
            $this->titulo = $sessao->modulos[$this->_controller];
            $this->view->titulo = $this->titulo;
            $this->view->title = $this->_titulo.' - '.$this->view->titulo;
            
            $perm_modulo = TRUE;
            
            $permissao = $sessao->modulos_permissao[$this->_controller];
            switch($this->_action) {
                case 'index':
                    if($permissao['MOD_ID'] > 0) $perm_modulo = TRUE;
                    else $perm_modulo = FALSE;
                    break;
                case 'add':
                    if($permissao['MOD_ADD'] == 1) $perm_modulo = TRUE;
                    else $perm_modulo = FALSE;
                    break;
                case 'edit':
                    $id = $this->_getParam('id', 0);
                    if($id == 0){ // add
                        if($permissao['MOD_ADD'] == 1) $perm_modulo = TRUE;
                        else $perm_modulo = FALSE;
                    } else { // edit
                        if($permissao['MOD_EDT'] == 1) $perm_modulo = TRUE;
                        else $perm_modulo = FALSE;
                    }
                    break;
                case 'delete':
                    if($permissao['MOD_DEL'] == 1) $perm_modulo = TRUE;
                    else $perm_modulo = FALSE;
                    break;
            }
            
            $this->view->per_add = $permissao['MOD_ADD'];
            $this->view->per_edt = $permissao['MOD_EDT'];
            $this->view->per_del = $permissao['MOD_DEL'];
            
        }
        else $this->view->title = $this->_titulo;
	  
        // dados de login do usuário em sessão
        //$auth = Zend_Auth::getInstance();
        //$s_user = $auth->getStorage();
        //echo '-'.$s_user->read()->USU_SESSAO;
        
        if($this->_controller <> 'index')
        {
            if($perm_modulo === FALSE)
            {
                include 'application/modules/painel/views/scripts/permissao.phtml';
                exit;                
            }else{ 
                //echo 'Possui permissão';
            }
            
        }
	
        // registro de acesso
        Zend_Loader::loadClass('estatisticas','library');
        Estatisticas::acesso_pagina($this->_getParam('id',0),1);

        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.js'); 
        /*
        //$this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/jquery-ui-1.10.2.custom/css/custom-theme/jquery-ui-1.10.2.custom.css'); 
        
        //$this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery-ui-1.10.2.custom/js/jquery-1.9.1.min.js');
        
        //$this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery-ui-1.10.2.custom/js/jquery-ui-1.10.2.custom.min.js');
        */
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/script_sis.js');
   
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/funcao.js');
        
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/css/base.css');
        
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/bootstrap/dist/css/bootstrap-theme.min.css');
        
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/bootstrap/dist/css/bootstrap.min.css');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/bootstrap/dist/js/bootstrap.min.js');
        
        
        
    }
    
    public function auth() {
        if (!Zend_Auth::getInstance()->hasIdentity() && empty(Zend_Auth::getInstance()->getStorage()->read()->USU_LOG)) {
            $this->_redirect($this->_modulo.'/auth');
        }
        /*
        Zend_Loader::loadClass('usuario','library');
        if(!Usuario::ValidarSessaoUsu()){
            $this->_helper->FlashMessenger('Login de acessso em uso!');
            Zend_Auth::getInstance()->clearIdentity();
            $this->_redirect($this->_modulo.'/auth');
        }
        */
        
    }
    
    public function paginacao($result,$paginas = 10) {

        $pagina = intval($this->_getParam('pagina', 1));
         
        $paginator = Zend_Paginator::factory($result);
        // Seta a quantidade de registros por página
        $paginator->setItemCountPerPage($paginas);
        // numero de paginas que serão exibidas
        $paginator->setPageRange(7);
        // Seta a página atual
        $paginator->setCurrentPageNumber($pagina);
        // Passa o paginator para a view
        $this->view->paginator = $paginator;
        
        return $paginator;
    }


}

?>

